# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=portaudio
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
epoch=1
pkgver=19.7.0
_tarver=190700_20210406
pkgrel=4
pkgdesc="A free, cross-platform, open source, audio I/O library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="http://www.portaudio.com"
license=("custom")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-pkg-config"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
options=(!libtool strip staticlibs)
source=("http://files.portaudio.com/archives/pa_stable_v${_tarver}.tgz"
        0001-clang-fix.patch
        0002-cmake-add-cpp-binding.patch
        0003-pa_getversioninfo-fix.patch
        CMakeLists.txt
        portaudiocpp.pc.in)
sha256sums=('47efbf42c77c19a05d22e627d42873e991ec0c1357219c0d74ce6a2948cb2def'
            '2c1ef1727d45f2140ef0a29ffa0553c7b9a1b59e1a223e8892a7792a860f2922'
            '22e485926fb875fba2a5c3011a1bbe5de99c1ef4560677105f767e66c8fb6324'
            '8f12f41692880c9dffb4f8147af2394df2bd0b2b762ae3fc09e1555eb27b9acb'
            '6af7c2c023483af698edd5063a0dce7cebb65eb40a256e6269ffd6ad32895dd4'
            'e48867fb8eb4ceda8e646879a03525b9bacbbaf154585c81478b06eef1c9d6a5')

prepare() {
  cd "${srcdir}/${_realname}"
  mkdir -v bindings/cpp/cmake
  cp -v "${srcdir}/CMakeLists.txt" bindings/cpp
  cp -v "${srcdir}/portaudiocpp.pc.in" bindings/cpp/cmake

  patch -p1 -i "${srcdir}/0001-clang-fix.patch"
  patch -p1 -i "${srcdir}/0002-cmake-add-cpp-binding.patch"
  patch -p1 -i "${srcdir}/0003-pa_getversioninfo-fix.patch"
}

build() {
  [[ -d "build-static-${MINGW_CHOST}" ]] && rm -rf "build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      ../${_realname}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build . --target install
}
